package Formularios;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.naming.LimitExceededException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.color.CMMException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class frmClientes extends JFrame {

	private JPanel contentPane;
	private JTextField txtNombre;
	private JTextField txtApellidoP;
	private JTextField txtApellidoM;
	public JTextField txtTelefono;
	private JTextField txtCelular;
	private JLabel lblStado;
	public JTextField txtCodEstado;
	public JTextField txtEstado;
	public JLabel lblMunicipio;
	public JTextField txtCodMunicipio;
	public JTextField txtMunicipio;
	public JTextField txtCodColonia;
	public JTextField txtColonia;
	private StatusBar sbMensaje;
	private JComboBox cbTipo;
	private int iMayus = 0;
	
	// teclas Virtuales
	static final int VK_SALTO_DE_LINEA = 10;
	static final int VK_ESPACIO = 32;
	static final int VK_F1 = 112;
	static final int VK_F5 = 116;
	static final int VK_SUPR = 127;
	//Tipo Busqueda
	static final int TIPO_ESTADOS = 1;
	static final int TIPO_MUNICIPIOS = 2;
	static final int TIPO_COLONIAS = 3;

	static Statement comando;
	static Connection con;
	private JLabel lblRfc;
	private JTextField txtRFC;
	private JLabel lblNewLabel;
	static frmClientes frame;
	private JTextField txtNumCliente;
	private JLabel lblEmail;
	private JTextField txtEmail;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new frmClientes();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public frmClientes() {
		setResizable(false);
		setTitle("Alta de Clientes");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 464, 358);
		// Centrar
		setLocationRelativeTo(null);
		
		general gen = new general();
		try {
			comando = gen.abrirConexion(cNemonicos.IP_GLOBAL,1);
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNombre = new JLabel("Nombre :");
		lblNombre.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNombre.setBounds(18, 45, 72, 15);
		contentPane.add(lblNombre);
		
		JLabel lblDomicilio = new JLabel("Domicilio");
		lblDomicilio.setHorizontalAlignment(SwingConstants.CENTER);
		lblDomicilio.setBounds(18, 72, 434, 15);
		contentPane.add(lblDomicilio);
		
		JLabel lblTelefonoPart = new JLabel("Tel. Part.");
		lblTelefonoPart.setBounds(16, 202, 72, 15);
		contentPane.add(lblTelefonoPart);
		
		JLabel lblTelCelular = new JLabel("Tel. Celular :");
		lblTelCelular.setBounds(231, 202, 95, 15);
		contentPane.add(lblTelCelular);
		
		txtNombre = new JTextField();
		txtNombre.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {

				char c = e.getKeyChar();
				if (((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
				if (txtNombre.getText().length() == 24) {
					e.consume();
				}
			}
			
			@Override
			public void keyPressed(KeyEvent e) {				
				if (e.getKeyCode() == VK_SALTO_DE_LINEA)
				{
					txtApellidoP.requestFocus();
				}
				if ( e.getKeyCode() == VK_SUPR )
				{
					int op = JOptionPane.showOptionDialog(null, "El cliente sera cancelado, desea continuar?",
							"Entrada Invalidad", JOptionPane.YES_NO_OPTION,
							JOptionPane.ERROR_MESSAGE, null,
							new Object[] { "ACEPTAR","CANCELAR" }, "ACEPTAR");
					if ( op == 0)
					{
						cancelarCliente(Integer.parseInt(txtNumCliente.getText()));
						limpiar();
					}			
				}
			}
		});
		txtNombre.setBounds(98, 39, 114, 19);
		contentPane.add(txtNombre);
		txtNombre.setColumns(10);
		txtNombre.setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(JComponent arg0) {
				if ( txtNombre.getText().length() > 0)
				{
					return true;
				}
				else
				{
					JOptionPane.showOptionDialog (null,"Favor de Capturar un Nombre","Datos Incorrectos",JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,new Object[] {"ACEPTAR"},"ACEPTAR");
					txtNombre.requestFocus();
					return false;
				}
				
			}
		});
		
		txtApellidoP = new JTextField();
		txtApellidoP.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if ( arg0.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA)
				{
					txtApellidoM.requestFocus();
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
				if (txtApellidoP.getText().length() == 19) {
					e.consume();
				}
			}
		});
		txtApellidoP.setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(JComponent arg0) {
				if ( txtApellidoP.getText().length() > 0)
				{
					return true;
				}
				else
				{
					JOptionPane.showOptionDialog (null,"Favor de Capturar un Apellido Paterno","Datos Incorrectos",JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,new Object[] {"ACEPTAR"},"ACEPTAR");
					txtApellidoP.requestFocus();
					return false;
				}
				
			}
		});
		txtApellidoP.setBounds(212, 39, 114, 19);
		contentPane.add(txtApellidoP);
		txtApellidoP.setColumns(10);
		
		txtApellidoM = new JTextField();
		txtApellidoM.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if ( e.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA)
				{
					txtCodEstado.requestFocus();
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
				if (txtApellidoM.getText().length() == 19) {
					e.consume();
				}
			}
		});
		txtApellidoM.setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(JComponent arg0) {
				if ( txtApellidoM.getText().length() > 0)
				{
					return true;
				}
				else
				{
					JOptionPane.showOptionDialog (null,"Favor de Capturar un Apellido Materno","Datos Incorrectos",JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,new Object[] {"ACEPTAR"},"ACEPTAR");
					txtApellidoM.requestFocus();
					return false;
				}
				
			}
		});
		txtApellidoM.setBounds(326, 39, 114, 19);
		contentPane.add(txtApellidoM);
		txtApellidoM.setColumns(10);
		
		txtTelefono = new JTextField();
		txtTelefono.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if (txtTelefono.getText().length() == 10) {
					e.consume();
				}

				char c = e.getKeyChar();
				if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
			}
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == VK_SALTO_DE_LINEA)
				{
					txtCelular.requestFocus();
				}
			}
		});
		txtTelefono.setBounds(85, 200, 114, 19);
		contentPane.add(txtTelefono);
		txtTelefono.setColumns(10);
		
		txtCelular = new JTextField();
		txtCelular.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == VK_SALTO_DE_LINEA)
				{
					txtRFC.requestFocus();
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				if (txtCelular.getText().length() == 10) {
					e.consume();
				}

				char c = e.getKeyChar();
				if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
			}
		});
		txtCelular.setBounds(324, 200, 114, 19);
		contentPane.add(txtCelular);
		txtCelular.setColumns(10);
		
		txtCodEstado = new JTextField();
		txtCodEstado.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				sbMensaje.setText("[ F1 ] Buscar Estados");
			}
		});
		txtCodEstado.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				switch ( arg0.getKeyCode() ) {
				case VK_SALTO_DE_LINEA:
						int iCodigo = Integer.parseInt(txtCodEstado.getText());
						String sTexto = general.conslutarNombreEstado(comando, iCodigo);
						txtEstado.setText(sTexto);
						txtCodMunicipio.requestFocus();
					break;
				case VK_F1:
					final frmBuscarDireccion form = new frmBuscarDireccion(frame);
					form.iTipoBusqueda = TIPO_ESTADOS;
					form.show();
					form.addWindowListener(new java.awt.event.WindowAdapter()   
					    {  
							public void windowClosed( java.awt.event.WindowEvent e )   
					        {  
					          int iCodigo = form.getCodigo();
					          txtCodEstado.setText(String.valueOf(iCodigo));
					          txtEstado.setText(form.getDescripcion());
					          txtCodMunicipio.requestFocus();
					        }  
					    }
					);
						
					break;

				default:
					break;
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (((!Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
				if (txtCodEstado.getText().length() == 2) {
					e.consume();
				}
			}
		});
		txtCodEstado.setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(JComponent arg0) {
				if ( txtEstado.getText().length() > 0)
				{
					return true;
				}
				else
				{
					JOptionPane.showOptionDialog (null,"Favor de Seleccionar un Estado","Datos Incorrectos",JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,new Object[] {"ACEPTAR"},"ACEPTAR");
					txtCodEstado.requestFocus();
					return false;
				}
				
			}
		});
		txtCodEstado.setBounds(98, 95, 48, 19);
		contentPane.add(txtCodEstado);
		txtCodEstado.setColumns(10);
		
		lblStado = new JLabel("Estado :");
		lblStado.setHorizontalAlignment(SwingConstants.RIGHT);
		lblStado.setBounds(18, 95, 72, 15);
		contentPane.add(lblStado);
		
		txtEstado = new JTextField();
		txtEstado.setEditable(false);
		txtEstado.setBounds(152, 93, 278, 19);
		contentPane.add(txtEstado);
		txtEstado.setColumns(10);
		
		lblMunicipio = new JLabel("Municipio :");
		lblMunicipio.setBounds(12, 124, 78, 15);
		contentPane.add(lblMunicipio);
		
		txtCodMunicipio = new JTextField();
		txtCodMunicipio.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				sbMensaje.setText("[ F1 Buscar Municipios ]");
			}
			
		});
		txtCodMunicipio.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				switch ( e.getKeyCode() ) {
				case VK_SALTO_DE_LINEA:
						int iCodigo = Integer.parseInt(txtCodMunicipio.getText());
						String sTexto = general.conslutarNombreMunicipio(comando, iCodigo);
						txtMunicipio.setText(sTexto);
						txtCodColonia.requestFocus();
					break;
				case VK_F1:
					final frmBuscarDireccion form = new frmBuscarDireccion(frame);
					form.iTipoBusqueda = TIPO_MUNICIPIOS;
					form.iEstado = Integer.parseInt(txtCodEstado.getText()); //se manda el estado para mostrar solo municipios de ese estado
					form.setTitle("Busqueda de Municipios");
					form.show();
					form.addWindowListener(new java.awt.event.WindowAdapter()   
					    {  
							public void windowClosed( java.awt.event.WindowEvent e )   
					        {  
					          int iCodigo = form.getCodigo();
					          txtCodMunicipio.setText(String.valueOf(iCodigo));
					          txtMunicipio.setText(form.getDescripcion());
					          txtCodColonia.requestFocus();
					        }  
					    }
					);
					break;

				default:
					break;
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (((!Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
				if (txtCodMunicipio.getText().length() == 5) {
					e.consume();
				}
			}
		});
		txtCodMunicipio.setBounds(98, 122, 48, 19);
		contentPane.add(txtCodMunicipio);
		txtCodMunicipio.setColumns(10);
		
		txtMunicipio = new JTextField();
		txtMunicipio.setEditable(false);
		txtMunicipio.setBounds(152, 124, 151, 19);
		contentPane.add(txtMunicipio);
		txtMunicipio.setColumns(10);
		
		sbMensaje = new StatusBar();
		sbMensaje.setHorizontalAlignment(SwingConstants.CENTER);
		sbMensaje.setBounds(0, 309, 462, 16);
		contentPane.add(sbMensaje);
		
		txtCodColonia = new JTextField();
		txtCodColonia.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				sbMensaje.setText("[ F1 ] Buscar Colonias");
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				sbMensaje.setText("[ ENTER ] Continuar");
			}
		});
		txtCodColonia.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				switch ( e.getKeyCode() ) {
				case VK_SALTO_DE_LINEA:
						int iCodigo = Integer.parseInt(txtCodColonia.getText());
						String sTexto = general.conslutarNombreColonia(comando, iCodigo);
						txtColonia.setText(sTexto);
						txtTelefono.requestFocus();
					break;
				case VK_F1:
					final frmBuscarDireccion form = new frmBuscarDireccion(
							frame);
					form.iTipoBusqueda = TIPO_COLONIAS;
					form.iMunicipio = Integer.parseInt(txtCodMunicipio
							.getText()); // se manda el municipio para mostrar
											// solo las colonias de ese
											// municipio
					form.setTitle("Busqueda de Colonias");
					form.show();
					form.addWindowListener(new java.awt.event.WindowAdapter() {
						public void windowClosed(java.awt.event.WindowEvent e) {
							int iCodigo = form.getCodigo();
							txtCodColonia.setText(String.valueOf(iCodigo));
							txtColonia.setText(form.getDescripcion());
							txtTelefono.requestFocus();
						}
					});
					break;
				default:
					break;
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (((!Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
					getToolkit().beep();
					e.consume();
				}
				if (txtCodColonia.getText().length() == 6) {
					e.consume();
				}
			}
		});
		txtCodColonia.setBounds(98, 151, 48, 19);
		contentPane.add(txtCodColonia);
		txtCodColonia.setColumns(10);
		
		JLabel lblColonia = new JLabel("Colonia :");
		lblColonia.setHorizontalAlignment(SwingConstants.RIGHT);
		lblColonia.setBounds(18, 153, 72, 15);
		contentPane.add(lblColonia);
		
		txtColonia = new JTextField();
		txtColonia.setEditable(false);
		txtColonia.setBounds(152, 151, 151, 19);
		contentPane.add(txtColonia);
		txtColonia.setColumns(10);
		
		lblRfc = new JLabel("RFC :");
		lblRfc.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRfc.setBounds(18, 232, 62, 15);
		contentPane.add(lblRfc);
		
		txtRFC = new JTextField();
		txtRFC.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == VK_SALTO_DE_LINEA)
				{
					txtEmail.requestFocus();
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				if (txtRFC.getText().length() == 13) {
					e.consume();
				}
			}
		});
		txtRFC.setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(JComponent arg0) {
				if ( txtRFC.getText().length() > 0)
				{
					return true;
				}
				else
				{
					JOptionPane.showOptionDialog (null,"Favor de Teclear un RFC Valido","Datos Incorrectos",JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,new Object[] {"ACEPTAR"},"ACEPTAR");
					txtRFC.requestFocus();
					return false;
				}
				
			}
		});
		txtRFC.setBounds(85, 230, 114, 19);
		contentPane.add(txtRFC);
		txtRFC.setColumns(10);
		
		lblNewLabel = new JLabel("Tipo :");
		lblNewLabel.setBounds(28, 263, 70, 15);
		contentPane.add(lblNewLabel);
		
		cbTipo = new JComboBox();
		cbTipo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if ( arg0.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA)
				{
					sbMensaje.setText("[ F5 ] Guardar");
				}
				if ( arg0.getKeyCode() == VK_F5)
				{
					if ( guardar() == true)
					{
						
					}
					limpiar();
				}
			}
		});
		cbTipo.setToolTipText("1 - Distribuidor directo de Fabrica");
		cbTipo.setModel(new DefaultComboBoxModel(new String[] {"1 - Distribuidor Directo", "2 - Distribuidor", "3 - Cliente final"}));
		cbTipo.setBounds(80, 260, 206, 24);
		contentPane.add(cbTipo);
		
		JLabel lblNumCliente = new JLabel("# Cliente: ");
		lblNumCliente.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNumCliente.setBounds(18, 12, 78, 15);
		contentPane.add(lblNumCliente);
		
		txtNumCliente = new JTextField();
		txtNumCliente.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				sbMensaje.setText("[ ENTER ] Continuar, [ F1 ] Buscar, [ F2 ] Registrar");
			}
		});
		txtNumCliente.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if ( e.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA )
				{
					if ( txtNumCliente.getText().length() == 0 )
					{
						txtNumCliente.setText(String.valueOf(general.consultarFolio(comando, cNemonicos.FOLIO_CLIENTES, cNemonicos.SIGUIENTE_SECUENCIA)));
						txtNombre.requestFocus();
					}
					else
					{
						boolean bContinuar = conslutarCliente(Integer.parseInt(txtNumCliente.getText()));
						if ( bContinuar == true )
						{
							txtNombre.requestFocus();
							sbMensaje.setText("[ ENTER ] Continuar,  [ SUPR ] Eliminar");
						}						
					}
				}
				if ( e.getKeyCode() == cNemonicos.VK_F1 )
				{
					final frmBuscarClientes form = new frmBuscarClientes();
					form.show();
					form.addWindowListener(new java.awt.event.WindowAdapter()   
				    {  
						public void windowClosed( java.awt.event.WindowEvent e )   
				        {  
				          int iCodigo = form.getCodigo();
				          txtNumCliente.setText(String.valueOf(iCodigo));
				          conslutarCliente(iCodigo);
				          txtNombre.requestFocus();
				        }  
				    }
				);
				}
			}
		});
		txtNumCliente.setBounds(98, 10, 62, 19);
		contentPane.add(txtNumCliente);
		txtNumCliente.setColumns(10);
		
		lblEmail = new JLabel("Email :");
		lblEmail.setBounds(212, 232, 70, 15);
		contentPane.add(lblEmail);
		
		txtEmail = new JTextField();
		txtEmail.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if ( e.getKeyCode() == cNemonicos.VK_SALTO_DE_LINEA)
				{
					cbTipo.requestFocus();
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (txtEmail.getText().length() == 50) {
					e.consume();
				}
			}
		});
		txtEmail.setBounds(260, 230, 192, 19);
		contentPane.add(txtEmail);
		txtEmail.setColumns(10);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{txtNumCliente, txtNombre, txtApellidoP, txtApellidoM, txtCodEstado, txtCodMunicipio, txtTelefono, txtCelular, txtRFC, txtEmail, cbTipo}));
	}
	
	public boolean guardar()
	{
		boolean bResultado = true;
		
		int iNumCliente = general.consultarFolio(comando, cNemonicos.FOLIO_CLIENTES, cNemonicos.INCREMENTAR_SECUENCIA);
		String sNombre = txtNombre.getText();
		String sApellidoP = txtApellidoP.getText();
		String sApellidoM = txtApellidoM.getText();
		int iEstado = Integer.parseInt(txtCodEstado.getText());
		int iMunicipio = Integer.parseInt(txtCodMunicipio.getText());
		int iColonia = Integer.parseInt(txtCodColonia.getText());
		String sTelefono = txtTelefono.getText();
		String sCelular = txtCelular.getText();
		String sRFC = txtRFC.getText();
		String sEmail = txtEmail.getText();
		String sTipo = (String) cbTipo.getSelectedItem();
		int iTipo = Integer.parseInt(sTipo.substring(0,1));

		String query;
		query = String.format("select GuardarCliente(" + iNumCliente + ",'%s','%s','%s', "+ iEstado + "," + iMunicipio + "," + iColonia + ", '%s','%s','%s','%s'," + iTipo + ")",
				sNombre, sApellidoP, sApellidoM,sTelefono,sCelular,sRFC,sEmail );
	
		cTotal insertar = new cTotal(comando);
		try {
			insertar.ejecutar(query);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			bResultado = false;
			general.grabarError("frmClientes", "guardar", -1, query, e.getMessage(),"Error al insertar el Cliente");
			e.printStackTrace();
		}

		return bResultado;		
	}
	
	public void limpiar()
	{
		txtNumCliente.requestFocus();
		txtNumCliente.setText("");
		txtNombre.setText("");
		txtApellidoP.setText("");
		txtApellidoM.setText("");
		txtCodEstado.setText("");
		txtEstado.setText("");
		txtCodMunicipio.setText("");
		txtMunicipio.setText("");
		txtCodColonia.setText("");
		txtColonia.setText("");
		txtTelefono.setText("");
		txtCelular.setText("");
		txtRFC.setText("");
		txtEmail.setText("");
		cbTipo.setSelectedIndex(0);
	}
	
	public boolean conslutarCliente(int iNumCliente)
	{
		boolean bResultado = true;

		try {

			// Una variable String para almacenar la sentencia SQL
			String query = "select trim(nombre) as nombre,trim(apellidop) as apellidop,trim(apellidom) as apellidom,estado,municipio,colonia,telefono,celular,rfc," +
					"trim(email) as email,tipo from caclientes where status = 1 and numcliente = " + iNumCliente + ";";

			ResultSet resultado = comando.executeQuery(query);

			// Ejecutamos la consulta SQL
			if (resultado.next()) 
			{
				txtNombre.setText(resultado.getString("nombre"));
				txtApellidoP.setText(resultado.getString("apellidop"));
				txtApellidoM.setText(resultado.getString("apellidom"));
				txtCodEstado.setText(String.valueOf(resultado.getInt("estado")));
				txtCodMunicipio.setText(String.valueOf(resultado.getInt("municipio")));
				txtCodColonia.setText(String.valueOf(resultado.getInt("colonia")));
				txtTelefono.setText(String.valueOf(resultado.getString("telefono")));
				txtCelular.setText(String.valueOf(resultado.getString("celular")));
				txtRFC.setText(resultado.getString("rfc"));
				txtEmail.setText(resultado.getString("email"));
				int iIndex = resultado.getInt("tipo");
				cbTipo.setSelectedIndex(iIndex-1);
			}
			else
			{
				bResultado = false;
			}

		} catch (SQLException e) {
			bResultado = false;
			// Por si ocurre un error
			System.out.println(e.getMessage());
			int op = JOptionPane.showOptionDialog(null, e.getMessage(),
					"Entrada Invalidad", JOptionPane.YES_OPTION,
					JOptionPane.ERROR_MESSAGE, null,
					new Object[] { "ACEPTAR" }, "ACEPTAR");
			if (op == 0) {
				//txtClave.requestFocus();
			}
		}
					
			
		return bResultado;		
	}
	public boolean cancelarCliente(int iNumCliente)
	{
		boolean bResultado = true;

		try {

			// Una variable String para almacenar la sentencia SQL
			String query = "update caclientes set status = 0 where numcliente = " + iNumCliente + ";";

			comando.executeUpdate(query);

			// Ejecutamos la consulta SQL
		} catch (SQLException e) {
			bResultado = false;
			// Por si ocurre un error
			System.out.println(e.getMessage());
			int op = JOptionPane.showOptionDialog(null, e.getMessage(),
					"Entrada Invalidad", JOptionPane.YES_OPTION,
					JOptionPane.ERROR_MESSAGE, null,
					new Object[] { "ACEPTAR" }, "ACEPTAR");
			if (op == 0) {
				//txtClave.requestFocus();
			}
		}
					
			
		return bResultado;		
	}
}
